//
//  AboutOurViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/9/13.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "AboutOurViewController.h"
#import "UIWebViewController.h"
#import "TSHeader.h"
#import "Singleton.h"
#import "ComMethod.h"
#import "CustomAlertView.h"

@interface AboutOurViewController ()<CustomAlertViewDelegate>

@end

@implementation AboutOurViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"关于我们";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _logoImageView.layer.cornerRadius = 4;
    _headerView.frame = CGRectMake(0, 0, 320, 150);
    self.tableView.tableHeaderView = _headerView;
    _versionLabel.text = [NSString stringWithFormat:@"V%@",[ComMethod getVersion]];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"AboutOurCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIndentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.textColor = MainTextColor;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.textColor = OrangeColor;
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"使用帮助";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"检查更新";
        if ([Singleton shareInstance].version.isHadNewVersion) {
            cell.detailTextLabel.text = [Singleton shareInstance].version.version;
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        } else {
            cell.detailTextLabel.text = @"已经是最新版本";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
//    else if (indexPath.row == 2) {
//        cell.textLabel.text = @"邀请好友";
//    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        UIWebViewController *webVC = [[UIWebViewController alloc] initWithNibName:@"UIWebViewController" bundle:nil];
        webVC.title = @"使用帮助";
        webVC.webUrl = [NSString stringWithFormat:@"%@/documents/tyxz_app_manual.html",[Singleton shareInstance].cityModel.manageSystemUrl];
        [self.navigationController pushViewController:webVC animated:YES];
    } else if (indexPath.row == 1) {
        if ([Singleton shareInstance].version.isHadNewVersion) {
            CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"版本更新" message:[Singleton shareInstance].version.updateLog delegate:self cancelButtonTitle:@"取消" sureButtonTitle:@"立即更新"];
            [alert show];
        }
    } else if (indexPath.row == 2) {
        
    }
}

#pragma mark CustomAlertViewDelegate
- (void)customAlertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[Singleton shareInstance].version.downloadUrl]];
    }
}

@end
